package com.bjb.vr.manager.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bjb.vr.common.ao.*;
import com.bjb.vr.common.result.BaseErrorCode;
import com.bjb.vr.common.result.BaseResult;
import com.bjb.vr.common.result.CommonResult;
import com.bjb.vr.common.service.ResBundleDetailService;
import com.bjb.vr.common.service.ResBundleService;
import com.bjb.vr.common.service.ResLocalResourceService;
import com.bjb.vr.common.service.ResPlatformResourceService;
import com.bjb.vr.common.vo.*;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 资源列表
 *
 * @version 1.0.0
 * @author: HuJingBo
 * @time: 2022/3/21 13:27
 */
@RestController
@RequestMapping("/api/manager/v1")
public class ResourceController {

    @Autowired
    ResPlatformResourceService platformResourceService;

    @Autowired
    ResLocalResourceService localResourceService;

    @Autowired
    ResBundleService bundleService;

    @Autowired
    ResBundleDetailService bundleDetailService;


    /**
     * 根据条件查询平台资源列表
     *
     * @param resPlatformAo
     * @return
     */
    @PostMapping("/platform/resources")
    public BaseResult<ResPlatformVo> getPlatformResources(@RequestBody ResPlatformAo resPlatformAo) {
        IPage<ResPlatformVo> resPlatformVoIPage = platformResourceService.getPlatformResources(resPlatformAo);
        return CommonResult.Ok().setResult(resPlatformVoIPage);
    }

    /**
     * 根据条件查询本地资源列表
     *
     * @param resLocalAo
     * @return
     */
    @PostMapping("/local/resources")
    public BaseResult<ResLocalVo> getLocalResources(@RequestBody ResLocalAo resLocalAo) {
        IPage<ResLocalVo> resLocalVoIPage = localResourceService.getLocalResources(resLocalAo);
        return CommonResult.Ok().setResult(resLocalVoIPage);
    }

    /**
     * 资源包列表
     *
     * @return
     */
    @PostMapping("/package/resources")
    public BaseResult<ResPackageVo> getPackageResources() {
        IPage<ResPackageVo> resPackageVos = bundleService.getPackageResources();
        return CommonResult.Ok().setResult(resPackageVos);
    }

    /**
     * 资源包详情
     *
     * @return
     */
    @PostMapping("/package/resources/detail")
    public BaseResult<ResPackageDetailVo> getPackageResourcesDetail(@RequestBody ResPackageDetailAo resPackageDetailAo) throws Exception {
        Assert.notNull(resPackageDetailAo.getResBundleId(), "资源包ID不能为空");
        IPage<ResPackageDetailVo> resPackageDetailVos = bundleDetailService.getPackageResourcesDetail(resPackageDetailAo);
        return CommonResult.Ok().setResult(resPackageDetailVos);
    }

    /**
     * 查询当前资源包以外的其他资源内容，然后进行添加操作
     *
     * @param resPackageDetailAo
     * @return
     * @throws Exception
     */
    @PostMapping("/package/resources/query")
    public BaseResult<ResPackageDetailVo> queryPackageResources(@RequestBody ResPackageDetailAo resPackageDetailAo) {
        Assert.notNull(resPackageDetailAo.getResBundleId(), "资源包ID不能为空");
        IPage<ResPackageResourcesVo> resPackageDetailVos = bundleDetailService.queryPackageResources(resPackageDetailAo);
        return CommonResult.Ok().setResult(resPackageDetailVos);
    }

    /**
     * 资源包添加新资源
     *
     * @param resPackageInsertAo
     * @return
     */
    @PostMapping("/package/resources/insert")
    public BaseResult<ResPackageDetailVo> insertPackageResources(@RequestBody ResPackageInsertAo resPackageInsertAo) {
        Assert.notNull(resPackageInsertAo.getResBundleId(), "资源包ID不能为空");
        Assert.notNull(resPackageInsertAo.getResArray(), "资源对象集合不能为空");
        boolean success = bundleDetailService.insertPackageResources(resPackageInsertAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_ADD);
        }
        return CommonResult.Ok();
    }

    /**
     * 删除之前把资源是否在我的收藏和课程列表做个集合返回
     *
     * @param resPackageDetailRemoveAo
     * @return
     */
    @PostMapping("/package/resources/beforeRemove")
    public BaseResult<ResPackageDetailVo> BeforeRemovePackageResources(@RequestBody ResPackageDetailRemoveAo resPackageDetailRemoveAo) {
        Assert.notNull(resPackageDetailRemoveAo.getResBundleId(), "资源包ID不能为空");
        Assert.notNull(resPackageDetailRemoveAo.getResIds(), "资源ID不能为空");
        List<ResPackageDetailVo> packageDetailVos = bundleDetailService.BeforeRemovePackageResources(resPackageDetailRemoveAo);
        return CommonResult.Ok().setResult(packageDetailVos);
    }

    /**
     * 资源包删除已有资源
     *
     * @param resPackageDetailRemoveAo
     * @return
     */
    @PostMapping("/package/resources/remove")
    public BaseResult<ResPackageDetailVo> removePackageResources(@RequestBody ResPackageDetailRemoveAo resPackageDetailRemoveAo) {
        Assert.notNull(resPackageDetailRemoveAo.getResBundleId(), "资源包ID不能为空");
        Assert.notNull(resPackageDetailRemoveAo.getResIds(), "资源ID不能为空");
        boolean success = bundleDetailService.removePackageResources(resPackageDetailRemoveAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_REMOVE);
        }
        return CommonResult.Ok();
    }

    /**
     * 判断资源包大小是否超过设备组内设备容量
     *
     * @param resPackageAo
     * @return
     */
    @PostMapping("/package/resources/judge")
    public BaseResult judgePackageCapcity(@RequestBody ResPackageAo resPackageAo) {
        Assert.notNull(resPackageAo.getCapacity(), "资源包容量不能为空");
        Assert.notNull(resPackageAo.getGroupIds(), "设备组ID不能为空");
        if ("null".equals(resPackageAo.getGroupIds())) {
            return CommonResult.Ok("资源大小未超过设备组容量").setResult(true);
        }
        return bundleDetailService.judgePackageCapcity(resPackageAo);
    }

}
